Security News
CISA Brings KEV Data to GitHub
CISA's KEV data is now on GitHub, offering easier access, API integration, commit history tracking, and automated updates for security teams and researchers.
The quick-lru package is a fast and efficient least-recently-used (LRU) cache implementation in JavaScript. It is designed to store a limited amount of items and evict the least-recently-used items once the limit is reached. This package is useful for caching results, reducing the number of expensive operations (like I/O), and improving application performance.
Set and Get Items
This feature allows you to set and retrieve items from the cache using the 'set' and 'get' methods. The 'maxSize' option specifies the maximum number of items the cache can hold.
{"const QuickLRU = require('quick-lru');
const lru = new QuickLRU({maxSize: 1000});
lru.set('key', 'value');
const value = lru.get('key'); // 'value'"}
Cache Eviction
This feature automatically evicts the least-recently-used items when the cache reaches its maximum size. In this example, after adding three items to a cache with a maxSize of 2, the first item is evicted.
{"const QuickLRU = require('quick-lru');
const lru = new QuickLRU({maxSize: 2});
lru.set('a', 1);
lru.set('b', 2);
lru.set('c', 3);
// At this point, 'a' is evicted because it is the least recently used item"}
Cache Deletion and Clearing
This feature allows you to delete individual items or clear the entire cache. The 'delete' method removes a specific item, and the 'clear' method removes all items.
{"const QuickLRU = require('quick-lru');
const lru = new QuickLRU({maxSize: 1000});
lru.set('key', 'value');
lru.delete('key'); // Removes 'key' from the cache
lru.clear(); // Removes all items from the cache"}
Cache Size and Existence Check
This feature provides methods to check the current size of the cache and whether a specific key exists in the cache. The 'size' property gives the number of items in the cache, and the 'has' method checks for the existence of a key.
{"const QuickLRU = require('quick-lru');
const lru = new QuickLRU({maxSize: 1000});
lru.set('key', 'value');
const size = lru.size; // 1
const hasKey = lru.has('key'); // true"}
lru-cache is another popular LRU cache implementation. It offers a similar set of features to quick-lru, such as set, get, eviction of old items, and item deletion. However, it has additional features like item expiration times, which quick-lru does not support.
node-cache is a simple caching module with set, get, and delete methods, similar to quick-lru. It supports TTL (time to live) for cache items and can be used as a plain key-value store. It is not strictly an LRU cache, but it provides similar caching capabilities.
tiny-lru is a minimal LRU cache implementation that is small in size. It provides basic LRU caching functionality like quick-lru but is designed to have a smaller footprint, making it suitable for environments where package size is a concern.
Useful when you need to cache something and limit memory usage.
Inspired by the hashlru
algorithm, but instead uses Map
to support keys of any type, not just strings, and values can be undefined
.
$ npm install quick-lru
const QuickLRU = require('quick-lru');
const lru = new QuickLRU({maxSize: 1000});
lru.set('🦄', '🌈');
lru.has('🦄');
//=> true
lru.get('🦄');
//=> '🌈'
Returns a new instance.
Type: object
Required
Type: number
The maximum number of items before evicting the least recently used items.
Optional
Type: (key, value) => void
Called right before an item is evicted from the cache.
Useful for side effects or for items like object URLs that need explicit cleanup (revokeObjectURL
).
The instance is iterable
so you can use it directly in a for…of
loop.
Both key
and value
can be of any type.
Set an item. Returns the instance.
Get an item.
Check if an item exists.
Get an item without marking it as recently used.
Delete an item.
Returns true
if the item is removed or false
if the item doesn't exist.
Delete all items.
Iterable for all the keys.
Iterable for all the values.
The stored item count.
FAQs
Simple “Least Recently Used” (LRU) cache
The npm package quick-lru receives a total of 14,648,852 weekly downloads. As such, quick-lru popularity was classified as popular.
We found that quick-lru demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
CISA's KEV data is now on GitHub, offering easier access, API integration, commit history tracking, and automated updates for security teams and researchers.
Security News
Opengrep forks Semgrep to preserve open source SAST in response to controversial licensing changes.
Security News
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.